using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

public class Player : NetworkBehaviour
{
    private Transform spawnedGameObject;

    private bool isDead;

    [SerializeField]
    private float moveSpeed = 5f;
    [SerializeField]
    private float jumpSpeed = 10f;

    [SerializeField]
    private LayerMask groundLayer;
    [SerializeField]
    private Transform groundCheckPivot;
    
    [SerializeField]
    private float groundCheckRadius = 0.2f;

    private Rigidbody2D rb = null;
    private bool isGround = true;

    private bool isJumpAction = false;

    private float lastDirX = 1;

    public override void OnNetworkSpawn()
    {
        if(IsServer) {
            Vector3 pos = PlayersManager.instance.GetSpawnPosition();
            PlayersManager.instance.RegisterPlayer(this);
            transform.position = pos;
        }

        if(!IsServer && IsOwner) {
            Debug.Log("本机玩家");
        }
    }

    private void OnDestroy() {
        if(IsServer && PlayersManager.instance != null) {
            PlayersManager.instance.UnregisterPlayer(this);
        }
    }

    void Awake() {
        // no = GetComponent<NetworkObject>();
    }

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        GameInput.instance.OnJumpAction += GameInput_OnJumpAction;
    }
    private void OnEnable() {
        // if()
        
    }

    private void GameInput_OnJumpAction() {
        if(!isJumpAction) {
            isJumpAction = true;
        }
    }

    private void OnDisable() {
        if(GameInput.instance != null) {
            GameInput.instance.OnJumpAction -= GameInput_OnJumpAction;
        } 
    }

    // Update is called once per frame
    void Update()
    {
        if(IsOwner) {
            Move();
            Jump();
            CheckGround();
            if(Input.GetKeyDown(KeyCode.T)) {
                // randomNumber.Value = Random.Range(10,500);
                // TestServerRpc(new ServerRpcParams());
                // 只给特定的客户端发送消息
                // TestClientRpc(new ClientRpcParams{
                //     Send = new ClientRpcSendParams(){TargetClientIds = new List<ulong> {1}}
                // });
                // Transform spawnObjectTransform = Instantiate(bombPrefab);
                // 随着场景销毁这个东西也会被销毁
                // spawnObjectTransform.GetComponent<NetworkObject>().Spawn(true);

                // spawnedGameObject = spawnObjectTransform;

                // randomNumber.Value = new MyCustomData{
                //     _int = Random.Range(10,500),
                //     _bool = false,
                //     message = "hello everybody!"
                // };
                // randomNumber._int = Random.Range(10,500);
            }

            if(Input.GetKeyDown(KeyCode.Y) && spawnedGameObject != null) {
                spawnedGameObject.GetComponent<NetworkObject>().Despawn();
                Destroy(spawnedGameObject);
            }
        }
    }

    private void Move() {
        Vector2 moveDir = GameInput.instance.GetMovementVectorNormolized();
        if(moveDir == Vector2.zero) return;

        rb.velocity = new Vector2(moveDir.x * moveSpeed,rb.velocity.y);
        lastDirX = moveDir.x;
        transform.localScale = new Vector2(lastDirX, 1);
    }

    public Vector2 GetPlayerVelocity() {
        return rb.velocity;
    }

    public bool GetPlayerIsGround() {
        return isGround && isJumpAction;
    }

    private void Jump() {
        if(isJumpAction && isGround) {
            Debug.Log("Jumping");
            isJumpAction = false;
            rb.AddForce(Vector2.up * jumpSpeed,ForceMode2D.Impulse);
        }
    }

    private void CheckGround() {
        isGround = Physics2D.OverlapCircle(groundCheckPivot.position,groundCheckRadius,groundLayer);
    }

    private void OnDrawGizmosSelected() {
        if(groundCheckPivot != null) {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(groundCheckPivot.position,groundCheckRadius);
        }    
    }
}
